// pages/mine/arrange.js
var app = getApp();
var api = require('../../api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datetime: ["3个月", "半年", "1年", "3年"],
    datetime_index: 0,
    effective: 365,
    project: [],
    project_index: 0,
    money: 200,
    num: 1,
    date: '2016-09-01',
    time: '10:00',
  },
  datetime_change: function(e) {
    var index = e.detail.value;
    this.setData({
      datetime_index: index,
    })
  },
  project_change: function(e) {
    var index = e.detail.value;
    this.setData({
      project_index: index,
    })
  },
  bind_num: function(e) {
    this.setData({
      num: e.detail.value,
    })
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function(e) {
    this.setData({
      time: e.detail.value
    })
  },
  formSubmit: function(e) {
    var info = {
      title: e.detail.value.title,
      project_id: this.data.project0[this.data.project_index].id,
      money: e.detail.value.money,
      number: e.detail.value.num,
      service_time: e.detail.value.effective,
      content: e.detail.value.content,
    };
    console.log(info)
    if (info.title == "") {
      wx.showToast({
        title: '请输入活动标题',
        icon: 'none'
      })
    } else if (info.content == "") {
      wx.showToast({
        title: '请输入活动内容',
        icon: 'none'
      })
    } else {

      app.ajax(api.add_active, {
        rd_session: wx.getStorageSync('rd_session'),
        title: e.detail.value.title,
        project_id: this.data.project0[this.data.project_index].id,
        money: e.detail.value.money,
        number: e.detail.value.num,
        service_time: e.detail.value.effective,
        content: e.detail.value.content,
      }, (res) => {
        if (res.result == 'success') {
          wx.showToast({
            title: '发布成功！',
            icon: 'none',
            duration: 0,
            mask: true,
            success: function(res) {
              wx.navigateTo({
                url: '/pages/active/list',
              })
            },
          })
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取数据
    app.ajax(api.shop_list, {
      rd_session: wx.getStorageSync('rd_session')
    }, (res) => {
      var project0 = res.data.project;
      var project_list = [];
      for (var i = 0; i < project0.length; i++) {
        project_list.push(project0[i].title)
      }
      this.setData({
        project: project_list,
        project_index: 0,
        project0: project0,
      })

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})